package com.zlm.user.emp.service.impl;

import com.zlm.common.base.AbstractBaseService;
import com.zlm.common.dto.HxUserDto;
import com.zlm.common.entity.UserRelationDO;
import com.zlm.huanxin.util.HuanxinUtils;
import com.zlm.user.emp.dao.UserRelationDao;
import com.zlm.user.emp.service.UserRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author: duyufu
 * @Date: 2019/7/8 13:25
 */
@Service
public class UserRelationServiceImpl extends AbstractBaseService<UserRelationDao, UserRelationDO> implements UserRelationService {

    @Autowired
    @Override
    public void setBaseDao(UserRelationDao userRelationDao) {
        this.baseDao = userRelationDao;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addFriend(String userHxId, String friendHxId) {
        Integer count = baseDao.countUserRelation(userHxId,friendHxId);
        if (count > 0) {
            //已存在好友关系，不做任何操作
        }else {
            //新增好友关系
            baseDao.insertUserRelation(userHxId,friendHxId);
            HuanxinUtils.addFriend(userHxId, friendHxId);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteFriend(String userHxId, String friendHxId) {
        baseDao.deleteUserRelation(userHxId,friendHxId);
        HuanxinUtils.deleteFriend(userHxId,friendHxId);
    }

    @Override
    public List<HxUserDto> queryFriendList(String hxId) {
        return baseDao.queryFriendList(hxId);
    }

}
